package servlet;

import cn.hutool.json.JSONUtil;
import dao.SaleStatisticDAO;
import vo.VisitVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * ClassName: DataViewServlet
 * Package: servlet
 * Description:
 *
 * @Author LIEX
 * @create 2024/7/16 0:23
 * @Version 1.0
 */
@WebServlet(name = "dataView", value = "/data/view")
public class DataViewServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SaleStatisticDAO statisticDAO=new SaleStatisticDAO();
        VisitVO visit = statisticDAO.findVisit();
        VisitVO userVisit = statisticDAO.getUserVisit();

        visit.setProductNum(userVisit.getProductNum());
        visit.setSaleNum(userVisit.getSaleNum());
        visit.setUserNum(userVisit.getUserNum());
        visit.setSaleMoney(userVisit.getSaleMoney());

        String jsonStr = JSONUtil.toJsonStr(visit);
        //返回结果
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        // 获取PrintWriter对象
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
        // 释放PrintWriter对象
        out.flush();
        out.close();

    }
}
